		
		$(document).ready(function(){
		  	var INTERV= 15000; //sell/buy should push one everyone so this should be enough
		  	var refresh;
		  	var model;
		  	
		  	jQuery("#webticker").webTicker();
		  	//im assuming this only gets executed on load once
		  	now.ready(function(){
		  		console.log("Exec: now.ready");
		  		//ask the server for object to initialize the viewmodel
		  		now.initGlobalModel();
		  		//get market info and set up interval
		  		refresh= setInterval("now.sendStuff()", INTERV);
		  	});

		  

		  	//setting up the global view model
		  	now.setGlobalModel= function(jsObj){
		  		console.log("setGlobalModel");
		  		model= new globalViewModel(jsObj);
		  		ko.applyBindings(model);
		  	}
		  
		  	//updates the view model
		 	now.receiveStuff = function(jsObj){
		    	console.log("Time rem: " + jsObj.remTime);
		    	//update the viewmodel
				model.remTime(jsObj.remTime).equities(jsObj.equities).traders(jsObj.traders).credit(now.credit).serverMsg(jsObj.msg);
				model.updatePortfolio();		//should insure that the portfolio info gets sent back to server every sell/buy
				copyToTicker();
				updateTicker();
		    	//time´s up??

		    	if(jsObj.remTime == 0){
		    		//stop counting and refreshing
		    		clearInterval(refresh);
		    		//but put in a on last request to start the chain for endgame
		    		console.log("Requesting Results from server")
		    		now.sendResults();
		    	}
		  	};
		  	now.exeOnConnect= function(){
		  		console.log("Exec serv: onconnect ")
		  	}
		  	
		  	now.recieveResult= function(obj){
		  		console.log("rendering recieveResults");
		  		console.log()
		  		var li;
		  		var rank=0;
		  		$('#webticker').empty();
		  		//$('#tickerTemp').empty();
		  		for(i=0;i<obj.length;i++){
		  			console.log(obj[i].name + " = " + obj[i].total);
		  			rank= i+1;
		  			if(i === 0)
		  				li= '<li class="green"><blink>'
		  			else
		  				li= '<li>'
		  			li+=  "#" + rank + ' : ' + obj[i].name + ' Total: ' + obj[i].total;
					if(1 === 0)
						li += '</blink></li>';
					else
						li += '</li>';
					$('#webticker').append(li);		  			
		  			
		  		}
		  		//$("#rslt").append($("#tickerTemp"));
		  	}
		  	
		  		  		  
		  	now.receiveMessage = function(name, message){
		    	$("#messages").prepend('<span class="traderName">' + name + '</span>: ' + message + '<br/>');
		  	}
			
			
		  	
		  	
			//having problems getting the desired result with KO so back to my trusted jQuery
			
			function genTickerStr(targ){
				var eqid= $(targ).children("#eq-id").html();
				var eqcurr= $(targ).children("#eq-currPr").html();
				var eqdir= $(targ).children("#eq-direction").html();
				var eqlast= $(targ).children("#eq-lastMove").html();
				var img="";
				if(eqdir === 'UP')
					img= '<img src="css/images/green.png"/>';
				else if(eqdir === 'DW')
					img= '<img src="css/images/red.png"/>'
				else
					eqdir= 'NC'
				var li= '<li class="' + eqid + '" title="' + eqid  + '" data="' + eqcurr + '">' + eqid + '&nbsp;' + eqcurr + '&nbsp;'+ img + eqlast + '%</li>'
				return li;
			};
			//trying to change the list while shit is loading
			//use to parse html and style it
			//need a better algo that swaps values instead of emptyinig
			function copyToTicker(){
				if($('#webticker').hasClass('initial')){
					var sl= $('#stocksList').children();
					var old= $("#webticker").empty().removeClass('initial');
					$(sl).each(function(index) {
						$("#webticker").append(genTickerStr(this));
					});
				}
			};
			
			function updateTicker(){
				var eqs= $('#stocksList').children();
				var tic= $("#webticker").children();
				$(eqs).each(function(index){
					$("#tickerTemp").append(genTickerStr(this));
				});
				var temp= $('#tickerTemp').children();				
				$(temp).each(function(index){
					tempId= $(this).attr('title');						//id of stock in update list
					ticObj= $(tic).filter('[title=' + tempId +']').first(); 	//get corresponding ticker li data
					ticData= $(ticObj).attr('data');  
					tempData= $(this).attr('data');
					if(tempData !== ticData){
						$(ticObj).replaceWith(this);
					}
				});
				$('#tickerTemp').empty();
				
			};
			
			$("#send-button").click(function(){
		    	now.distributeMessage($("#text-input").val());
		  	  	$("#text-input").val("").focus();
		  	});
		  	
		  	$('#text-input').bind('keypress', function(e) {
        		if(e.keyCode==13){
                	now.distributeMessage($("#text-input").val());
		  	  		$("#text-input").val("");
        		}
			});
		  	
		  	
		  	$(".change").click(function(){
		    	now.changeRoom($(this).text());
		  	});
		 
		  	$('button').live().button();
			
		  	$('.inputValue').live('keyup', function(){
				$(this).removeClass('red');
				if(isNaN($(this).val()))
					$(this).addClass('red');
				console.log("jquery change: " + $(this).val());
				
				var parent= $(this).parent().siblings().children(".transActBtn");
				$(parent).button();
				//arg!!!knockout does only sets disabled, not disabled="disabled", had to add a class 
				$(parent).not('.enabled').addClass('ui-button-disabled ui-state-disabled').removeClass('nudge ui-state-hover');
				$(parent).filter('.enabled').removeClass('ui-button-disabled ui-state-disabled').addClass('nudge ui-state-hover');
				
			});
			now.setup= function(){
				now.name= prompt("What's your name?", "");
		  		now.portfolio= new Array;
		  		//$('#webticker').removeClass('dsp-none');
		  	}		  
		  	now.setup();
		  	
	});